package titt.service.invoke;

import titt.head.HTTPHead; 
import titt.head.request.MethodHead;
import titt.head.request.Request;
import titt.head.request.RequestHead;
import titt.type.TypedObject;
import titt.type.Types;

public class UnknownMethodRedirect implements MethodParser {

	private final String methodName;
	private final MethodParser parser;
	
	public UnknownMethodRedirect(String methodName, MethodParser parser) {
		this.methodName = methodName;
		this.parser = parser;
	}
	
	@Override
	public MethodHead parse(HTTPHead head) {
		
		RequestHead r = new Request(head);

		if(r.getPath().indexOf("?") == -1) {
			
			String path = r.getPath().equals("/")? "/index.html" : r.getPath();
			TypedObject a = new TypedObject(Types.STRING, path);
			TypedObject b = new TypedObject(Types.toNative(RequestHead.class), r);
			
			return new MethodHead(methodName, new TypedObject[]{a,b}, new TypedObject[]{}, r);
		}

		return parser.parse(r);
	}

}
